const { Server } = require("socket.io");

var app = require('../app');
var http = require('http');
let {talkroomModel}=require('../modu/lyl')


var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

function normalizePort(val) {
    var port = parseInt(val, 10);
    if (isNaN(port)) {
        return val;
    }
    if (port >= 0) {
        return port;
    }
    return false;
}


var server = http.createServer(app);

var io = new Server(server, {
    cors: {
        origin: '*'
    },
    connectionStateRecovery:{}
});

io.on('connection', (socket) => {
    // let nowroom = ''
    socket.on('chatMessage',async (msg) => {
        await talkroomModel.create({ username: msg.username, info: msg.info, roomid: msg.roomid })
        io.emit('message',{msg:msg})
    });
    
    //加入房间
    // socket.on('joinroom',(data)=>{
    //     if(nowroom != ''){
    //         socket.leave(nowroom);
    //     }
    //     socket.join(data.roomid)
    //     nowroom = data.roomid
    // })

})



server.listen(port);
module.exports = server;
